package com.mccken.designpattern.chain.base;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-02-16 13:59
 **/
public abstract class McckenChainHandler {
	protected McckenChainHandler handler;
	
	private void next(McckenChainHandler handler) {
		this.handler = handler;
	}
	
	public static Builder builder() {
		return new Builder();
	} 
	
	public abstract void doHandle(IMcckenChainContext context);
	
	public static class Builder {
		private McckenChainHandler head;
		private McckenChainHandler tail;
		
		public Builder addHandler(McckenChainHandler handler) {
			if (head == null) {
				this.head = this.tail = handler;
				return this;
			}
		
			this.tail.next(handler);
			this.tail = handler;
			return this;
		}
		
		public McckenChainHandler build() {
			return this.head;
		}
	}
}
